package com.furnitur.common.service.impl;

import com.furnitur.common.enums.ResourceType;
import com.furnitur.common.service.DataPermissionService;
import com.furnitur.core.mapper.product.FurnitureMapper;
import com.furnitur.core.mapper.order.OrderMapper;
import com.furnitur.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataPermissionServiceImpl implements DataPermissionService {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private FurnitureMapper furnitureMapper;
    
    @Autowired
    private OrderMapper orderMapper;
    
    @Override
    public boolean verifyResourceOwnership(ResourceType resourceType, Long resourceId) {
        Long currentMerchantId = getCurrentMerchantId();
        if (currentMerchantId == null || resourceId == null) {
            return false;
        }
        
        switch (resourceType) {
            case FURNITURE:
                return furnitureMapper.verifyOwnership(resourceId, currentMerchantId);
            case ORDER:
                return orderMapper.verifyOwnership(resourceId, currentMerchantId);
            case MERCHANT:
                return resourceId.equals(currentMerchantId);
            default:
                return false;
        }
    }
    
    @Override
    public Long getCurrentMerchantId() {
        return userService.getCurrentMerchantId();
    }
} 